package com.globalLogic.gps.graph;

import java.util.List;

import com.globalLogic.gps.graph.algorithms.Algorithm;

public class Graph {
	private INode[] nodes; // objetos que implementan inode
	private  int[][] graph; // matriz de weights entre nodes
	private Algorithm algorithm;
	public Graph() {
	}
	/**
	 * Inicializa el grafo en base a una seria de nodos.
	 * @param serieNodes
	 */
	public void inicializarGraph(List <INode> serieNodes) {
		
		nodes = (INode[])serieNodes.toArray(new INode[serieNodes.size()]);
		graph = new int[nodes.length][nodes.length];
	}
	
	
	public void addRoute(String from, String to, int weight) {
		int n1 = positionNode(from);
		int n2 = positionNode(to);
		graph[n1][n2] = weight;
		graph[n2][n1] = weight;
	}
	public int positionNode(String pId ) {
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].getId().equals(pId))
				return i;
		}
		return -1;
	}
	
	public INode[] getNodes() {
		return nodes;
	}

	public void setNodes(INode[] nodes) {
		this.nodes = nodes;
	}

	public int[][] getGraph() {
		return graph;
	}

	public void setGrafo(int[][] graph) {
		this.graph = graph;
	}
	public int distanceBetweenNodes (String from, String to)
	{
		int n1 = positionNode(from);
		int n2 = positionNode(to);
		return graph[n1][n2]; 
	}
	public Algorithm getAlgorithm() {
		return algorithm;
	}
	public void setAlgorithm(Algorithm algorithm) {
		this.algorithm = algorithm;
		algorithm.setGraph(this);
	}
	public  String minimumRoute(String from, String to)
	{
		
		return getAlgorithm().minimumRoute(from, to);
	}

}